package com.anrongtec.zcpt.interfaces.theInferfaces.callback;

import android.app.Activity;
import android.app.ProgressDialog;
import android.text.TextUtils;

import com.art.base.util.DialogUtil;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.request.BaseRequest;


/**
 * ================================================
 * 作    者：jeasonlzy（廖子尧）
 * 版    本：1.0
 * 创建日期：2016/4/8
 * 描    述：我的Github地址  https://github.com/jeasonlzy0216
 * 修订历史：
 * ================================================
 */
public abstract class StringDialogCallback extends StringCallback {

    private ProgressDialog dialog;

    public StringDialogCallback(Activity activity, String msg) {
    	if (TextUtils.isEmpty(msg)) {
    		dialog = DialogUtil.createLoadingDialog(activity, "请稍候...");
		}else {
			dialog = DialogUtil.createLoadingDialog(activity, msg);
		}
    }

	public StringDialogCallback() {

	}

	@Override
    public void onBefore(BaseRequest request) {
        super.onBefore(request);
        //网络请求前显示对话框
        if (dialog != null && !dialog.isShowing()) {
        	  dialog.show();
        }
    }

    @Override
	public void onAfter(String t, Exception e) {
		super.onAfter(t, e);
		dismissDialog();
	}

    @Override
    public void onError(okhttp3.Call call, okhttp3.Response response,
    		Exception e) {
    	super.onError(call, response, e);
    	dismissDialog();
    }

    private void dismissDialog() {
		//网络请求结束后关闭对话框
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
	}
    
}
